import { Component, markRaw } from 'vue';
import { FormItemType, FormItemConfig } from '../types';

// 导入各表单组件
import FormInput from './FormInput.vue';
import FormSelect from './FormSelect.vue';
import FormDatePicker from './FormDatePicker.vue';
import FormRadioGroup from './FormRadioGroup.vue';
import FormCheckboxGroup from './FormCheckboxGroup.vue';
import FormTreeSelect from './FormTreeSelect.vue';
import FormNumberInput from './FormNumberInput.vue';
import FormSwitch from './FormSwitch.vue';
import FormCascader from './FormCascader.vue';

// 组件映射
const componentMap: Record<FormItemType, Component> = {
  [FormItemType.INPUT]: markRaw(FormInput),
  [FormItemType.SELECT]: markRaw(FormSelect),
  [FormItemType.DATE_PICKER]: markRaw(FormDatePicker),
  [FormItemType.RADIO_GROUP]: markRaw(FormRadioGroup),
  [FormItemType.CHECKBOX_GROUP]: markRaw(FormCheckboxGroup),
  [FormItemType.TREE_SELECT]: markRaw(FormTreeSelect),
  [FormItemType.NUMBER]: markRaw(FormNumberInput),
  [FormItemType.SWITCH]: markRaw(FormSwitch),
  [FormItemType.CASCADER]: markRaw(FormCascader),
  [FormItemType.CUSTOM]: null as unknown as Component, // 自定义组件在配置中提供
};

/**
 * 获取表单项对应的组件
 * @param item 表单项配置
 * @returns 对应的表单组件
 */
export const getFormItemComponent = (item: FormItemConfig): Component => {
  if (item.type === FormItemType.CUSTOM) {
    return item.component;
  }
  
  return componentMap[item.type];
};

// 导出所有表单子组件
export {
  FormInput,
  FormSelect,
  FormDatePicker,
  FormRadioGroup,
  FormCheckboxGroup,
  FormTreeSelect,
  FormNumberInput,
  FormSwitch,
  FormCascader,
};